class Weblog < ActiveRecord::Base
  has_many                :holders
  has_many                :categories
  has_many                :albums
  has_many                :entries,  :through => :holders,    :order => 'id DESC'
  has_many                :articles, :through => :categories, :order => 'id DESC', :source => :entries
  has_many                :photos,   :through => :albums,     :order => 'id DESC', :source => :entries
  has_many                :links
  has_and_belongs_to_many :users

  def archives(time_zone, monthly = true)
    entries.group_by { |e| monthly ? time_zone.utc_to_local(e.created_at).beginning_of_month : time_zone.utc_to_local(e.created_at).at_midnight }
  end

  def comments
    entries.collect(&:comments).flatten.sort { |x, y| y.id <=> x.id }
  end

  def messages
    entries.collect(&:messages).flatten.sort { |x, y| x.id <=> y.id }
  end

  def trackbacks
    entries.collect(&:trackbacks).flatten.sort { |x, y| x.id <=> y.id }
  end
end
